package pers.lance.exam.util;

/**
 * @ Author     ：LILA3
 * @ Date       ：Created in 10:38 PM 7/30/2018
 */

import java.io.*;

public class FileUtil {


    public static void copyFile(File from, File to) throws IOException {

        if (!from.exists()) {
            System.out.println("file is not exists");
            return;
        }
        if (!to.exists()) {
            if (!to.getParentFile().exists()) {
                to.getParentFile().mkdirs();
            }
            to.createNewFile();
        }
        if (!from.isFile()) {
            System.out.println("The source file address is not a file");
            return;
        } else if (!to.isFile()) {
            System.out.println("The target file address is not a file");
            return;
        }


        FileInputStream fis = new FileInputStream(from);
        FileOutputStream fos = new FileOutputStream(to);

        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] buf = new byte[1024];

        while ((bis.read(buf)) != -1) {
            bos.write(buf, 0, buf.length);
        }

        bos.flush();
        bis.close();
        bos.close();
    }
}

